package cn.portx.view;

import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import com.bstek.bdf3.security.ContextUtils;
import com.bstek.dorado.core.Configure;
import com.bstek.dorado.view.View;
import com.bstek.dorado.web.DoradoContext;

@Component
public class BaoxianPR {

	public void onInit(View view) throws Exception {
		String sso_uid = ContextUtils.getLoginUsername();
		String sso_timestamp = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS");
		String sso_sign = DigestUtils.md5Hex(sso_uid + sso_timestamp + Configure.getString("sso.des.key")).toUpperCase();

		byte[] deskey = Configure.getString("sso.des.key").getBytes();
		DESKeySpec dks = new DESKeySpec(deskey);
		//实例化密钥工厂  
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		//生成密钥  
		SecretKey secretKey = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		sso_uid = Base64.encodeBase64String(cipher.doFinal(sso_uid.getBytes()));

		DoradoContext.getCurrent().setAttribute(DoradoContext.VIEW, "sso_uid", sso_uid);
		DoradoContext.getCurrent().setAttribute(DoradoContext.VIEW, "sso_timestamp", sso_timestamp);
		DoradoContext.getCurrent().setAttribute(DoradoContext.VIEW, "sso_sign", sso_sign);
	}

}
